<?php defined('SYS_PATH') or die('No direct script access.');
/**
 * Request class
 *
 * @author zendzhang<zendzhang@hotmail.com>
 * @version $Id: Request.php 9 2013-01-14 03:24:50Z zendzhang@hotmail.com $
 */
class TrueBlood_Request extends TrueBlood
{
    /**
     * Fetch an item from the POST/GET array
     *
     * @param string $key
     * @param mixed $default
     * @return mixed
     */
    public static function input($key, $default = null)
    {
        if (isset($_POST[$key]))
        {
            return trim($_POST[$key]);
        } elseif (isset($_GET[$key]))
        {
            return trim($_GET[$key]);
        }
        return $default;
    }

    /**
     * Fetch an item from the GET array
     *
     * @param string $key
     * @param mixed $default
     * @return mixed
     */
    public static function get($key = null, $default = null)
    {
        if (null === $key)
        {
            return $_GET;
        }
        return (isset($_GET[$key])) ? trim($_GET[$key]) : $default;
    }

    /**
     * Fetch an item from the POST array
     *
     * @param string $key
     * @param mixed $default
     * @return mixed
     */
    public static function post($key = null, $default = null)
    {
        if (null === $key)
        {
            return $_POST;
        }
        return (isset($_POST[$key])) ? trim($_POST[$key]) : $default;
    }

    /**
     * Fetch an item from the COOKIE array
     *
     * @param string $key
     * @param mixed $default
     * @return mixed
     */
    public static function cookie($key = null, $default = null)
    {
        return self::$load->cookie->get($key, $default);
    }

    /**
     * Fetch an item from the SERVER array
     *
     * @param string $key
     * @param mixed $default
     * @return mixed
     */
    public static function server($key = null, $default = null)
    {
        if (null === $key)
        {
            return $_SERVER;
        }
        return (isset($_SERVER[$key])) ? trim($_SERVER[$key]) : $default;
    }

    /**
     * Fetch an item from the SESSION array
     *
     * @param string $key
     * @param mixed $default
     * @return mixed
     */
    public static function session($key = null, $default = null)
    {
        isset($_SESSION) || session_start();
        if (null === $key)
        {
            return $_SESSION;
        }
        return (isset($_SESSION[$key])) ? trim($_SESSION[$key]) : $default;
    }

    /**
     * Removes slashes
     *
     * @param mixed $var
     * @return mixed
     */
    public static function stripslashes($var)
    {
        if (!get_magic_quotes_gpc())
        {
            return $var;
        }
        return is_array($var) ? array_map('stripslashes', $var) : stripslashes($var);
    }

    /**
     * String cleaner, to prevent any kind of injection
     *
     * @param string $str
     * @return string
     */
    public static function clean($str)
    {
        return $str; // TODO
    }

    /**
     * Fetch client IP
     *
     * @param string $default
     * @return string
     */
    public static function clientIp($default = '0.0.0.0')
    {
        $directIP = empty($_SERVER['REMOTE_ADDR']) ? $default : $_SERVER['REMOTE_ADDR'];
        $proxys = array
        (
            'HTTP_X_FORWARDED_FOR',
            'HTTP_X_FORWARDED',
            'HTTP_FORWARDED_FOR',
            'HTTP_FORWARDED',
            'HTTP_VIA',
            'HTTP_X_COMING_FROM',
            'HTTP_COMING_FROM'
        );
        $proxyIP = '';
        foreach ($proxys as $proxy)
        {
            if (isset($_SERVER[$proxy]) && !empty($_SERVER[$proxy]))
            {
                $proxyIP = $_SERVER[$proxy];
                break;
            }
        }
        $isIP = preg_match('|^([0-9]{1,3}\.){3,3}[0-9]{1,3}|', $proxyIP, $regs);
        if ($isIP && (count($regs) > 0))
        {
            return $regs[0];
        }
        else
        {
            return $directIP;
        }
    }

    /**
     * Get current url
     *
     * @return string
     */
    public static function currentUrl()
    {
        $domain = self::domain();
        $port = self::server('SERVER_PORT');
        if (80 != $port)
        {
            $domain .= ":{$port}";
        }
        return $domain . self::server('REQUEST_URI');
    }

    /**
     * get the domain with or without protocol
     *
     * @param boolean $url
     * @return string
     */
    public static function domain($protocol = true)
    {
        if (!$protocol)
        {
            return self::server('SERVER_NAME');
        }
        if (defined(SITE_URL))
        {
            return SITE_URL;
        }
        else
        {
            $http = 'http';
            if ('on' == strtolower(self::server('HTTPS')))
            {
                $http = 'https';
            }
            return "{$http}://" . self::server('SERVER_NAME');
        }
    }
}

?>